//*****************************************窗口面板*****************************************
/*
config: {
    id:''         必须输入、ID唯一
    title:'',     缺省：'SoData'
    top:'',       缺省：无-居中
    left:'',      缺省：无-居中
    width:'',     缺省：'240'
    height:'',    缺省：'150'
    resizable:true  true:可缩放，缺省：无
    isCanMin:true   true:可最小化，缺省：无
    isCanMax:true   true:可最大化，缺省：无
    model:'',     '0'-模式窗口，'1'-非模式窗口，缺省：'0'
    status:'0',   窗口状态，0:正常化，1：最大化，2：最小化，缺省：'0'
    buttons       {align:'center', data:[{type:'ok', text:'确定'}]}，缺省：无，
}
*/
function RxDialog(config, callback) {
    this.rxtype = "RxDialog";
    this.config = {};
    this.config.id = config.id;
    this.config.title = RxCommon.toDefault(config.title, 'DActive');
    this.config.top = RxCommon.toDefault(config.top, '');
    this.config.left = RxCommon.toDefault(config.left, '');
    this.config.width = RxCommon.toDefault(config.width, '240');
    this.config.height = RxCommon.toDefault(config.height, '150');
    this.config.resizable = RxCommon.toDefault(config.resizable, false);
    this.config.isCanMin = RxCommon.toDefault(config.isCanMin, false);
    this.config.isCanMax = RxCommon.toDefault(config.isCanMax, false);
    this.config.model = RxCommon.toDefault(config.model, '0');
    this.config.status = RxCommon.toDefault(config.status, '0');
    this.config.buttons = RxCommon.toDefault(config.buttons, {});
    this.config.buttons.align = RxCommon.toDefault(this.config.buttons.align, 'center');
    this.config.buttons.data = RxCommon.toDefault(this.config.buttons.data, []);
    this.callback = callback;

    this.init();
    this.initEvent();
    this.initData();
}

RxDialog.prototype = {
    init: function() {
        this.windowStatusOld = '0';/*窗口状态，0:正常化，1：最大化，2：最小化*/
        this.windowStatus = '0';/*窗口状态，0:正常化，1：最大化，2：最小化*/
        this.windowArea = {};
        this.dragLooper = 0;
        this.loopTimer = null;
        this.point = {x:0 , y:0};
        this.controls = [];
        if (this.config.model == "0") {
            this.createMask();
        }
        this.createDialog();
        this.createPanel();
        if (this.config.buttons.data.length > 0) {
            this.createButton();
        }
        this.createTitle();
        this.createContentPanel();
    },
    initEvent: function() {
        if (this.config.resizable == true) {
            this.dialog.onmousemove = function(e) {
                e = e || window.event;
                var target = e.srcElement || e.currentTarget || e.target;
                var self = target._self;
                if (!self || self.rxtype !== "RxDialog") {
                    return;
                }
                this.style.cursor = self.getCursorType(e);
            };
            this.dialog.onmousedown = this.dragStart;
        }
        this.titlePanel.onmousedown = this.dragStart;
        if (this.config.isCanMax == true) {
            this.titleMax.onclick = function() {
                this._self.windowStatusOld = this._self.windowStatus;
                if (this._self.windowStatus == '1') {
                    this._self.windowStatus = '0'
                } else {
                    this._self.windowStatus = '1'
                }
                this._self.setWindowStatus();
            };
        }
        if (this.config.isCanMin == true) {
            this.titleMin.onclick = function() {
                this._self.windowStatusOld = this._self.windowStatus;
                if (this._self.windowStatus == '2') {
                    this._self.windowStatus = '0'
                } else {
                    this._self.windowStatus = '2'
                }
                this._self.setWindowStatus();
            };
        }
        this.titleClose.onclick = function() {
            var rxevent = {};
            rxevent.name = "onclose";
            rxevent.self = this._self._self;
            rxevent.target = this;
            rxevent.source = this._self;
            this._self.hide();
            if (this._self.callback) {
            	this._self.callback(rxevent);
            }
            this._self.dispose(true);
        };
        if (this.buttons && this.buttons.length > 0) {
            for (var i = 0; i < this.buttons.length; i++) {
                this.buttons[i].onclick = function() {
                    var rxevent = {};
                    rxevent.name = "onclick";
                    rxevent.self = this._self._self;
                    rxevent.target = this;
                    rxevent.source = this._self;
                    rxevent.type = this.data.type;
                    rxevent.text = this.data.text;
                    if (this.data.type == "cancel") {
                    	this._self.hide();
                    }
                    var result = this._self.callback(rxevent);
                    if (result) {
                        this._self.dispose(true);
                    }
                };
            }
        }
    },
    initData: function() {
        if (this.config.status && this.config.status != this.windowStatus) {
            if (this.config.status == "1" && this.config.isCanMax == true) {
                this.titleMax.click();
            } else if (this.config.status == "2" && this.config.isCanMin == true) {
                this.titleMin.click();
            } else {
                this.resize();
            }
        } else {
            this.resize();
        }
    },
    setWindowStatus: function() {
        if (this.windowStatus == '0') {
            this.titleMax.className = "rxdialog_title_max";
            this.titleMin.className = "rxdialog_title_min";
            this.restoreWindowSize();
        } else if (this.windowStatus == '1') {
            this.titleMax.className = "rxdialog_title_max_restore";
            this.titleMin.className = "rxdialog_title_min";
            this.backupWindowSize();
            this.config.top = 0;
            this.config.left = 0;
            this.config.width = document.documentElement.clientWidth;
            this.config.height = document.documentElement.clientHeight;
        } else if (this.windowStatus == '2') {
            this.titleMax.className = "rxdialog_title_max";
            this.titleMin.className = "rxdialog_title_min_restore";
            this.backupWindowSize();
            this.config.top = document.documentElement.clientHeight-56;
            this.config.left = 0;
            this.config.width = 210;
            this.config.height = 56;
        }
        this.resize();
    },
    backupWindowSize: function() {
        if (this.windowStatusOld == "0") {
            this.windowArea = {};
            this.windowArea.top = this.config.top;
            this.windowArea.left = this.config.left;
            this.windowArea.width = this.config.width;
            this.windowArea.height = this.config.height;
        }
    },
    restoreWindowSize: function() {
        this.config.top = this.windowArea.top;
        this.config.left = this.windowArea.left;
        this.config.width = this.windowArea.width;
        this.config.height = this.windowArea.height;
        this.windowArea = {};
    },
    show: function() {
        if (this.dialogMask) {
            this.dialogMask.style.display = "block";
        }
        this.dialog.style.display = "block";
    },
    hide: function() {
        if (this.dialogMask) {
            this.dialogMask.style.display = "none";
        }
        this.dialog.style.display = "none";
    },
    createMask: function() {
        this.dialogMask = this.ce(document.body, '_rxdialog_mask_' + this.config.id, "div", "rxdialog_mask");
        this.dialogMask.style.zIndex = "1000";
    },
    createDialog: function() {
        this.dialog = this.ce(document.body, '_rxdialog_' + this.config.id, "div", "rxdialog");
        this.dialog.isDialog = true;
        this.dialog.style.width = this.config.width + "px";
        this.dialog.style.height = this.config.height + "px";
        this.dialog.style.zIndex = "1000";
    },
    createPanel: function() {
        this.panel = this.ce(this.dialog, null, "div", "rxdialog_panel");
    },
    createTitle: function() {
        this.titlePanel = this.ce(this.panel, null, "div", "rxdialog_title_panel");
        this.titleText = this.ce(this.titlePanel, null, "div", "rxdialog_title_text");
        if (this.config.isCanMax == true) {
            this.titleMax = this.ce(this.titlePanel, null, "div", "rxdialog_title_max");
        }
        if (this.config.isCanMin == true) {
            this.titleMin = this.ce(this.titlePanel, null, "div", "rxdialog_title_min");
        }
        this.titleClose = this.ce(this.titlePanel, null, "div", "rxdialog_title_close");

        this.titleText.innerHTML = this.config.title;
        RxCommon.disableSelection(this.titleText);
    },
    createContentPanel: function() {
        this.contentPanel = this.ce(this.panel, null, "div", "rxdialog_text");
        if (this.config.buttons.data.length == 0) {
            this.contentPanel.style.bottom = "10px";
        }
    },
    createButton: function() {

        this.buttonLine = this.ce(this.panel, null, "div", "rxdialog_button_line");
        this.buttonPanel = this.ce(this.panel, null, "div", "rxdialog_button_panel");
        this.buttonsPanel = this.ce(this.buttonPanel, null, "div", "rxdialog_buttons_panel");

        this.buttons = [];
        var panelWidth = 0;
        var buttonData = this.config.buttons.data;
        for (var i = 0; i < buttonData.length; i++) {
            var button = this.ce(this.buttonsPanel, null, "div", "rxdialog_rxbutton");

            button.innerHTML = buttonData[i].text;
            button.style.top = "0px";
            button.style.left = (panelWidth) + "px";
            var buttonWidth = RxCommon.toDefault(buttonData[i].width, 80);
            button.style.width = (buttonWidth) + "px";
            if (i < buttonData.length - 1) {
                panelWidth = panelWidth + buttonWidth + 10;
            } else {
                panelWidth = panelWidth + buttonWidth;
            }
            button.data = buttonData[i];

            this.buttons[i] = button;
        }
        this.buttonsPanel.style.width = panelWidth + "px";
        this.buttonsPanel.panelWidth = panelWidth;
    },
    resize: function() {
        if (RxCommon.isEmpty(this.config.top)) {
            this.config.top = ((document.documentElement.clientHeight-this.config.height)/2);
        }
        if (RxCommon.isEmpty(this.config.left)) {
            this.config.left = ((document.documentElement.clientWidth-this.config.width)/2);
        }
        this.dialog.style.top = (this.config.top) + "px";
        this.dialog.style.left = (this.config.left) + "px";
        this.dialog.style.width = (this.config.width) + "px";
        this.dialog.style.height = (this.config.height) + "px";
        if (this.config.buttons.data.length > 0) {
            if (this.config.buttons.align == "left") {
                this.buttonsPanel.style.left = 0 + "px";
            } else if (this.config.buttons.align == "right") {
                this.buttonsPanel.style.right = 0 + "px";
            } else {
                this.buttonsPanel.style.left = ((this.config.width-40 - this.buttonsPanel.panelWidth)/2) + "px";
            }
        }
        for (var i = 0; i < this.controls.length; i++) {
            if (this.controls[i].resize) {
                this.controls[i].resize();
            }
        }
    },
    ce: RxCommon.ce,
    addPage: function(url) {
        this.iframe = this.ce(this.contentPanel, "_iframe_" + this.config.id, "iframe");
        this.iframe.style.cssText = "position:absolute;top:0px;left:0px;width:100%;height:100%;margin:0;padding:0;";
        this.iframe.setAttribute("frameBorder", "0");
        this.iframe.setAttribute("border", "0");
        if (this.iframe.attachEvent) {
            this.iframe.attachEvent("onload", function() {
                console.log("loaded...1");
            });
        } else {
            this.iframe.onload = function() {
                console.log("loaded...2");
            };
        }
        this.iframe.src = url;
    },
    getPageData: function(type) {
        if (this.iframe) {
            var doc = this.iframe.contentDocument || this.iframe.contentWindow.document;
            return doc.onPageReturn(type);
        }
        return null;
    },
    setPageData: function(data) {
    	document.paramData = data;
    },
    dispose: function(isAll) {
        for (var i = 0; i < this.controls.length; i++) {
            if (this.controls[i].dispose) {
                this.controls[i].dispose(true);
            }
        }
        this.controls = [];
        if (isAll) {
            while (this.dialog.hasChildNodes()) {
                this.dialog.removeChild(this.dialog.lastChild);
            }
            document.body.removeChild(this.dialog);
            if (this.dialogMask) {
                document.body.removeChild(this.dialogMask);
            }
        }
    },
    dragStart: function(e) {
        e = e || window.event;
        var target = e.srcElement || e.currentTarget || e.target;
        var self = target._self;
        if (!self || self.rxtype !== "RxDialog") {
            return;
        }
        self.dragMode = (self.dialog == target ? "resize" : "move");
        self.dragCursor = self.getCursorType(e);
        if (self.dragMode == "resize" && self.dragCursor == "default") {
            self.dragMode = null;
            self.dragCursor = null;
            return false;
        }
        window.rxself = self;

        document.documentElement.onselectstart = function(){return false;};
        document.documentElement.ondragstart = function(){return false;};
        document.documentElement.onmousemove = self.dragMove;
        document.documentElement.onmouseup = self.dragDrop;

        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
        self.point.x = e.clientX/1 + 5 + scrollLeft;
        self.point.y = e.clientY/1 + 5 + scrollTop;

        self.dragLooper = 0;
        self.dragTimer();
    },
    dragMove: function(e) {
        e = e || window.event;
        var target = e.srcElement || e.currentTarget || e.target;
        var self = window.rxself;

        if(self.dragLooper < 10) {
            return;
        }

        var clientHeight = document.documentElement.clientHeight
        var clientWidth = document.documentElement.clientWidth
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;

        var dragDrop_x = e.clientX/1 + 5 + scrollLeft;
        var dragDrop_y = e.clientY/1 + 5 + scrollTop;

        var ctrlTop = self.dialog.offsetTop + scrollTop;
        var ctrlLeft = self.dialog.offsetLeft + scrollLeft;
        var dialogTop = ctrlTop + dragDrop_y - self.point.y;
        var dialogLeft = ctrlLeft + dragDrop_x - self.point.x;

        if (self.dragMode == "move") {
            if ((dialogTop < -41 || dialogTop > clientHeight-15)
                && (dialogLeft < -self.config.width+15 || dialogLeft > clientWidth-15)) {
                return false;
            } else if (dialogTop < -41 || dialogTop > clientHeight-15) {
                self.config.left = (dialogLeft);
            } else if (dialogLeft < -self.config.width+15 || dialogLeft > clientWidth-15) {
                self.config.top = (dialogTop);
            } else {
                self.config.left = (dialogLeft);
                self.config.top = (dialogTop);
            }
        } else {
            if (self.dragCursor.charAt(0) == "n") {
                self.config.height = parseInt(self.config.height) - (dragDrop_y - self.point.y);
                self.config.top = parseInt(self.config.top) + (dragDrop_y - self.point.y);
            } else if (self.dragCursor.charAt(0) == "s") {
                self.config.height = parseInt(self.config.height) + (dragDrop_y - self.point.y);
            }
            if (self.dragCursor.charAt(0) == "w" || self.dragCursor.charAt(1) == "w") {
                self.config.width = parseInt(self.config.width) - (dragDrop_x - self.point.x);
                self.config.left = parseInt(self.config.left) + (dragDrop_x - self.point.x);
            } else if (self.dragCursor.charAt(0) == "e" || self.dragCursor.charAt(1) == "e") {
                self.config.width = parseInt(self.config.width) + (dragDrop_x - self.point.x);
            }
            if (self.config.width <= 210) {
                self.config.width = 210;
            }
            if (self.config.height <= 56) {
                self.config.height = 56;
            }
        }

        self.dialog.style.left = (self.config.left) + 'px';
        self.dialog.style.top = (self.config.top) + 'px';
        self.dialog.style.width = (self.config.width) + 'px';
        self.dialog.style.height = (self.config.height) + 'px';

        self.point.x = dragDrop_x;
        self.point.y = dragDrop_y;

        return false;
    },
    dragDrop: function(e) {
        e = e || window.event;
        var target = e.srcElement || e.currentTarget || e.target;
        var self = window.rxself;
        if (self.loopTimer){
            clearTimeout(self.loopTimer);
        }
        self.dragLooper = -1;
        self.resetMouseEvent();
        if (self.dragMode == "resize") {
            self.resize();
        }
        self.dragMode = null;
        self.dragCursor = null;
        window.rxself = null;
    },
    dragTimer: function() {
        var self = window.rxself;
        if(self.dragLooper >= 0 && self.dragLooper < 10) {
            self.dragLooper = self.dragLooper + 1;
            if (self.loopTimer){
                clearTimeout(self.loopTimer);
            }
            self.loopTimer = setTimeout(function(){
                self.dragTimer();
            }, 10);
            return;
        }
    },
    resetMouseEvent: function() {
        document.documentElement.onselectstart = function(){return true;};
        document.documentElement.ondragstart = function(){return true;};
        document.documentElement.onmousemove = function(){return true;};
        document.documentElement.onmouseup = function(){return true;};
    },
    getCursorType: function(e) {
        e = e || window.event;
        var target = e.srcElement || e.currentTarget || e.target;
        var resizeCursor = "default";
        if (!target.isDialog) {
            return resizeCursor;
        }

        var topOffset = e.clientY - target.offsetTop;
        var leftOffset = e.clientX - target.offsetLeft;
        var bottomOffset = target.offsetTop + target.clientHeight - e.clientY;
        var rightOffset = target.offsetLeft + target.clientWidth - e.clientX;

        if ((topOffset > 0 && topOffset < 10) && (leftOffset > 0 && leftOffset < 10)) {
            resizeCursor = "nw-resize";
        } else if ((topOffset > 0 && topOffset < 10) && (rightOffset > 0 && rightOffset < 10)) {
            resizeCursor = "ne-resize";
        } else if ((bottomOffset > 0 && bottomOffset < 10) && (leftOffset > 0 && leftOffset < 10)) {
            resizeCursor = "sw-resize";
        } else if ((bottomOffset > 0 && bottomOffset < 10) && (rightOffset > 0 && rightOffset < 10)) {
            resizeCursor = "se-resize";
        } else if (topOffset > 0 && topOffset < 10) {
            resizeCursor = "n-resize";
        } else if (bottomOffset > 0 && bottomOffset < 10) {
            resizeCursor = "s-resize";
        } else if (leftOffset > 0 && leftOffset < 10) {
            resizeCursor = "w-resize";
        } else if (rightOffset > 0 && rightOffset < 10) {
            resizeCursor = "e-resize";
        }
        return resizeCursor;
    },
    add: function(content, style) {
        if (content) {
            if (Object.prototype.toString.call(content) === "[object Array]") {
                for (var i = 0; i < content.length; i++) {
                    this.contentPanel.appendChild(content[i]);
                    this.controls[this.controls.length] = content[i];
                }
            } else {
                if (style) {
                    RxCommon.setStyle(content, style);
                }
                this.contentPanel.appendChild(content);
                this.controls[this.controls.length] = content;
            }
        }
    },
    remove: function(content) {
        if (content) {
            if (Object.prototype.toString.call(content) === "[object Array]") {
                for (var i = 0; i < content.length; i++) {
                    var ctrl = this.find(content[i]);
                    if (ctrl) {
                        this.contentPanel.removeChild(ctrl);
                    }
                }
            } else {
                var ctrl = this.find(content);
                if (ctrl) {
                    this.contentPanel.removeChild(ctrl);
                }
            }
        }
    },
    find: function(content) {
        if (Object.prototype.toString.call(content)==="[object String]") {
            for (var i = 0; i < this.controls.length; i++) {
                if (this.controls[i].id == content) {
                    return this.controls[i];
                }
            }
            return null;
        } else {
            return content;
        }
    },
    clear: function() {
        this.dispose();
    }
};
